package com.myway.service.ibe.command;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.myway.dto.Result.SearchResult;
import com.myway.dto.domain.FlightCriteria;
import com.myway.dto.domain.SegmentAndPassengerParam;
import com.myway.dto.ibe_param.*;
import com.myway.service.ibe.SearchOneService;
import com.travelsky.ebuild.clientapi.searchone.SearchOne;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by yufei on 2018/7/22.
 */
public class SearchOneCommand extends BaseIBECommand implements SearchOneService {


    public String execute(SegmentAndPassengerParam spp, boolean multiCabin ) {
        SearchOneParam param = new SearchOneParam();
        try{
            List<SegmentParam> segments = spp.getSegmentParams();
            //将航段集合添加到searchone属性
            param.setSegments(segments);
            //添加货币代码
            param.setDisplayCurrency("USD");
            //代理信息
            List<AgenciesParam> agencies = new ArrayList<AgenciesParam>();
            AgenciesParam agency = new AgenciesParam();
            agencies.add(agency);
            //将代理信息添加到searchone
            param.setAgencies(agencies);
            //旅客信息
            Map<String, Integer> passengerCount = spp.getPassengerCount();
            List<PassengersParam> passengers = new ArrayList<PassengersParam>();
            for (int i = 0; i < passengerCount.get("adult"); i++){
                PassengersParam passenger = new PassengersParam();
                String[] ptc =new String[1];
                ptc[0]="ADT";
                passenger.setPtc(ptc);
                passenger.setVirtual(false);
                passengers.add(passenger);
            }
            if (passengerCount.get("child") > 0 && passengerCount.get("child") != null){
                for (int i = 0;i < passengerCount.get("child"); i++){
                    PassengersParam passenger = new PassengersParam();
                    String[] ptc =new String[1];
                    ptc[0]="CNN";
                    passenger.setPtc(ptc);
                    passenger.setVirtual(false);
                    Map<String,Integer> dateOfBirth = new HashMap<String,Integer>();
                    Calendar cal = Calendar.getInstance();
                    dateOfBirth.put("day",cal.get(Calendar.DATE));
                    dateOfBirth.put("month",cal.get(Calendar.MONTH));
                    dateOfBirth.put("year",cal.get(Calendar.YEAR)-6);
                    passenger.setDateOfBirth(dateOfBirth);
                    passengers.add(passenger);
                }
            }
            if (passengerCount.get("infant") > 0 && passengerCount.get("infant") != null){
                for (int i = 0; i < passengerCount.get("infant"); i++){
                    PassengersParam passenger = new PassengersParam();
                    String[] ptc =new String[1];
                    ptc[0]="INF";
                    passenger.setPtc(ptc);
                    passenger.setVirtual(false);
                    Map<String,Integer> dateOfBirth = new HashMap<String,Integer>();
                    Calendar cal = Calendar.getInstance();
                    dateOfBirth.put("day",cal.get(Calendar.DATE));
                    dateOfBirth.put("month",cal.get(Calendar.MONTH));
                    dateOfBirth.put("year",cal.get(Calendar.YEAR)-1);
                    passenger.setDateOfBirth(dateOfBirth);
                    passengers.add(passenger);
                }
            }
            param.setPassengers(passengers);
            //行程偏好
            JourneyPreferencesParam journeyPreferencesParam = new JourneyPreferencesParam();
            String[] fareTypes = new String[2];
            fareTypes[0] = "PUBLIC";
            fareTypes[1] = "PRIVATE";
            journeyPreferencesParam.setFareTypes(fareTypes);
            journeyPreferencesParam.setMultiCabin(multiCabin);
            param.setJourneyPreferences(journeyPreferencesParam);
            //旅客偏好信息
            PreferencesParam preferencesParam = new PreferencesParam();
            //设置舱位等级
            CabinsParam cabinsParam = new CabinsParam();
            String[] codes = new String[3];
            codes[0] = "BUSINESS";
            codes[1] = "ECONOMY";
            codes[2] = "FIRST";
            cabinsParam.setCodes(codes);
            String[] faresAllowedCarriers = new String[1];
            faresAllowedCarriers[0]="MJ";

            preferencesParam.setFaresAllowedCarriers(faresAllowedCarriers);
            cabinsParam.setVirtual(false);
            preferencesParam.setCabins(cabinsParam);
            param.setPreferences(preferencesParam);

            String json = JSON.toJSONString(param);
            SearchOne one = new SearchOne();
            this.configIBEClientPrice(one);
            String response = one.getSearchOneStringRes(json);
           /* System.out.println("======searchOneRequest======" + json + "===========");
            System.out.println("======searchOneResponse======" + response + "===========");*/
            return response;
        }catch(Exception e){
            e.printStackTrace();
            System.out.println( e.getMessage());
        }
        return null;
    }
    public SearchResult executeNew(FlightCriteria criteria, boolean multiCabin ) {
        SearchOneParam param = new SearchOneParam();
        SearchResult result = null;
        try{
            String travlType = criteria.getTravlType();
            List<SegmentParam> segments = Lists.newArrayList();
            //航段1（去程）
            SegmentParam segmentParamOne = new SegmentParam();
            //判断是否是日历查询
            int from =criteria.getFrom();
            int to  = criteria.getTo();
            if(from!=0||to!=0){
                FlexInterval flex = new FlexInterval();
                flex.setFrom(from);
                flex.setTo(to);
                segmentParamOne.setFlexInterval(flex);
            }
            //设置起飞时间
            String strDate = criteria.getFlightDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date depDate = sdf.parse(strDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(depDate);
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH)+1;
            /*System.out.print("---getPrice---"+month);*/
            int day = calendar.get(Calendar.DATE);
            DepatureDateParam depatureDateParam = new DepatureDateParam();
            depatureDateParam.setYear(year);
            depatureDateParam.setMonth(month);
            depatureDateParam.setDay(day);
            segmentParamOne.setDepartureDate(depatureDateParam);
            //设置出发地
            List<Origin> origins = Lists.newArrayList();
            Origin origin = new Origin();
            origin.setName(criteria.getOrgCity());
            origins.add(origin);
            segmentParamOne.setOrigin(origins);
            //设置目的地
            List<Destination> destinations = Lists.newArrayList();
            Destination destination = new Destination();
            destination.setName(criteria.getDstCity());
            destinations.add(destination);
            segmentParamOne.setDestination(destinations);
            segments.add(segmentParamOne);
            if(travlType.equals("rt")) {
                //返程
                SegmentParam segmentParamTwo = new SegmentParam();
                //设置返程起飞时间
                String strArrDate = criteria.getArrDate();
                Date arrDate = sdf.parse(strArrDate);
                calendar.setTime(arrDate);
                int yearArr = calendar.get(Calendar.YEAR);
                int monthArr = calendar.get(Calendar.MONTH) + 1;
                /*System.out.print("---getPrice---" + monthArr);*/
                int dayArr = calendar.get(Calendar.DATE);
                DepatureDateParam depatureDateParamTwo = new DepatureDateParam();
                depatureDateParamTwo.setYear(yearArr);
                depatureDateParamTwo.setMonth(monthArr);
                depatureDateParamTwo.setDay(dayArr);
                segmentParamTwo.setDepartureDate(depatureDateParamTwo);
                //设置出发地
                List<Origin> originsArr = Lists.newArrayList();
                Origin originArr = new Origin();
                originArr.setName(criteria.getDstCity());
                originsArr.add(origin);
                segmentParamOne.setOrigin(originsArr);
                //设置目的地
                List<Destination> destinationsArr = Lists.newArrayList();
                Destination destinationArr = new Destination();
                destinationArr.setName(criteria.getOrgCity());
                destinationsArr.add(destinationArr);
                segmentParamOne.setDestination(destinationsArr);
                segments.add(segmentParamTwo);
            }
            //将航段集合添加到searchone属性
            param.setSegments(segments);
            //添加货币代码
            //param.setDisplayCurrency("EUR");
            //代理信息
            List<AgenciesParam> agencies = new ArrayList<AgenciesParam>();
            AgenciesParam agency = new AgenciesParam();
            agencies.add(agency);
            //将代理信息添加到searchone
            param.setAgencies(agencies);
            //旅客信息
            List<PassengersParam> passengers = new ArrayList<PassengersParam>();
            PassengersParam passenger = new PassengersParam();
            String[] ptc =new String[1];
            ptc[0]="ADT";
            passenger.setPtc(ptc);
            passenger.setVirtual(false);
            passengers.add(passenger);
            param.setPassengers(passengers);
            //行程偏好
            JourneyPreferencesParam journeyPreferencesParam = new JourneyPreferencesParam();
            String[] fareTypes = new String[2];
            fareTypes[0] = "PUBLIC";
            fareTypes[1] = "PRIVATE";
            journeyPreferencesParam.setFareTypes(fareTypes);
            journeyPreferencesParam.setMultiCabin(multiCabin);
            param.setJourneyPreferences(journeyPreferencesParam);
            //旅客偏好信息
            PreferencesParam preferencesParam = new PreferencesParam();
            //设置舱位等级
            CabinsParam cabinsParam = new CabinsParam();
            String[] codes = new String[3];
            codes[0] = "BUSINESS";
            codes[1] = "ECONOMY";
            codes[2] = "FIRST";
            cabinsParam.setCodes(codes);
            String[] faresAllowedCarriers = new String[1];
            faresAllowedCarriers[0]="MJ";

            preferencesParam.setFaresAllowedCarriers(faresAllowedCarriers);
            cabinsParam.setVirtual(false);
            preferencesParam.setCabins(cabinsParam);
            param.setPreferences(preferencesParam);

            String json = JSON.toJSONString(param);
            SearchOne one = new SearchOne();
            this.configIBEClientPrice(one);
            String response = one.getSearchOneStringRes(json);
            result = JSON.parseObject(response, SearchResult.class);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println( e.getMessage());
        }
        return result;
    }
}
